package com.googlecode.androidspamblocker;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import android.content.Context;
import android.os.Bundle;
import android.telephony.SmsMessage;

public class SmsCompositeFilter implements SmsFilter {
	private static final String TAG = "SmsCompositeFilter";
	private static final String DELIMITER = ",";
	private Pattern addressPattern;
	private Pattern bodyPattern;
	private int action;
	private int hits = 0;
	private boolean enabled = true;;
	
	public SmsCompositeFilter() {
	}
	
	public SmsCompositeFilter(String address, String body, int action) throws PatternSyntaxException {
		addressPattern = Pattern.compile(address);
		bodyPattern = Pattern.compile(body);
		this.action = action;
	}
	
	public String getAddress() {
		return addressPattern.pattern();
	}

	public void setAddressPattern(String address) {
		this.addressPattern = Pattern.compile(address);
	}

	public String getBody() {
		return bodyPattern.pattern();
	}

	public void setBodyPattern(String body) {
		this.bodyPattern = Pattern.compile(body);
	}

	public int getAction() {
		return action;
	}

	public void setAction(int action) {
		this.action = action;
	}

	public boolean isEnabled() {
		return enabled;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public int getHits() {
		return hits;
	}

	public int filter(SmsMessage msg, Context context) {
		String address = msg.getOriginatingAddress();
		String body = msg.getMessageBody();
		Matcher addressMatcher = addressPattern.matcher(address);
		Matcher bodyMatcher = bodyPattern.matcher(body);
		if(enabled && addressMatcher.find() && bodyMatcher.find()) {
			hits++;
			return action;
		}
		return SmsAction.UNKNOWN;
	}
	
	public int filter(SmsMessage[] msg, Context context) {
		String address = msg[0].getOriginatingAddress();
		StringBuilder builder = new StringBuilder();
		for(int i = 0; i < msg.length; i++) {
			builder.append(msg[i].getMessageBody());
		}
		Matcher addressMatcher = addressPattern.matcher(address);
		Matcher bodyMatcher = bodyPattern.matcher(builder.toString());
		if(enabled && addressMatcher.find() && bodyMatcher.find()) {
			hits++;
			return action;
		}
		return SmsAction.UNKNOWN;
	}
	
	public void load(Bundle bundle) throws PatternSyntaxException {
		String addr = bundle.getString("address");
		String body = bundle.getString("body");
		int act = bundle.getInt("action", SmsAction.UNKNOWN);
		addressPattern = Pattern.compile(addr);
		bodyPattern = Pattern.compile(body);
		action = act;
	}
	
	public void load(String filter) throws PatternSyntaxException, NumberFormatException {
		String[] fields = filter.split(DELIMITER);
		addressPattern = Pattern.compile(fields[0]);
		bodyPattern = Pattern.compile(fields[1]);
		action = Integer.parseInt(fields[2]);
		hits = Integer.parseInt(fields[3]);
		enabled = Boolean.parseBoolean(fields[4]);
	}
	
	public String toString() {
		return addressPattern.pattern()
			+ DELIMITER + bodyPattern.pattern()
			+ DELIMITER + action
			+ DELIMITER + hits
			+ DELIMITER + enabled;
	}

}
